/**
 * Created by Quaye on 2016/12/21.
 * WebSocket_Client 1.0:
 *
 */

function JisightLookAtMeClient(selfUserId) {
    var m_lookSelf = this;
    /***************************************
     *                pubulic              *
     **************************************/
    m_lookSelf.TryConnect = m_OnStTryConnect;
    m_lookSelf.JLAM_Event_Fir = null;
    m_lookSelf.GetGameStatus = m_Get_GameStatus;
    m_lookSelf.SndMsgToOther = m_Push_MsgInteraction;
    /***************************************
     *                private              *
     **************************************/
    var m_userId = selfUserId;
    var m_ip = null;
    var m_port = null;
    var m_roomId = null;
    var m_gameSign = null;
    var m_webSocet = null;

    /****************************************
     *                 Function             *
     ****************************************/
    function m_OnStTryConnect(ip, port, roomId, gameSign) {
        debug.out("------ TryConnect in ------");
        console.log("------ TryConnect in ------");
        if (typeof (ip) != "string" || (typeof(port) != "string" && typeof (port) != "number")) {
            debug.outWarning("Your param is wrong please check them. \n" +
                "Ip must be 'string'\n" +
                "Port must be 'number' or 'string'");
            debug.outWarning(" Ip   :" + ip);
            debug.outWarning(" Port :" + port);
        }
        else {
            m_ip = ip;
            m_port = port;
            m_roomId = roomId;
            m_gameSign = gameSign;
            var eventArgs = wsEventArgs;
            eventArgs.status = LAM_STATUS.DEFALUT_VALUE;
            if (undefined == window.WebSocket) {
                eventArgs.message = "Your Device is not support WebSocket!";
                eventArgs.status = LAM_STATUS.ERROR_TRIGGER_CODE;
                debug.outWarning(eventArgs.message);
            } else {
                try {
                    m_webSocet = new window.WebSocket("ws://" + m_ip + ":" + m_port);
                    m_webSocet.onopen = m_OnOpenFunc;
                    console.log("------ open------");

                }
                catch (ex) {
                    debug.outWarning("There is some Error.When try connect .");
                    debug.outWarning(ex);
                    eventArgs.message = "Your Device is not support WebSocket!";
                    eventArgs.status = LAM_STATUS.FAILED_CONNECT;
                }
            }
            m_EventHandler(eventArgs);
        }
        debug.out("------ TryConnect out  ------");
    }

    function m_OnOpenFunc() {
        debug.out("------ On WSOpen in  ------");
        console.log("------ On WSOpen in  ------");
        m_Post_RoomVerification(m_roomId, m_gameSign);
        debug.out("------ On WSOpen out ------");
        m_webSocet.onmessage = m_OnMsgFunc;
        m_webSocet.onclose = m_OnCloseFunc;
        m_webSocet.onerror = m_OnErrorFunc;
    }

    function m_OnMsgFunc(args) {
        debug.out("------ On WS MSG in  ------");
        debug.out("<<<<<<<<<<<  [Rev Original data] : " + args.data);
        var eventArgs = wsEventArgs;
        eventArgs.status = LAM_STATUS.DEFALUT_VALUE;
        var jsonData = JSON.parse(args.data);

        if (!jsonData.Tp) {                                                     // ----Tp
            debug.outWarning("there is no 'Tp',please check data from sever.");
        }
        else {
            switch (jsonData.Tp) {
                case JISIGHT_LAM_RECV_TYPE.ROOM_VERIFY_T:           //31000 房间验证成功
                    eventArgs.message = "Room verification success.";
                    debug.out("Room verification success.");
                    eventArgs.status = LAM_STATUS.DATA_ROOM_VERIFY_T;
                    break;
                case JISIGHT_LAM_RECV_TYPE.ROOM_VERIFY_F:           //31001 房间验证失败
                    eventArgs.message = "Room verification failed.";
                    debug.out("Room verification failed.");
                    eventArgs.status = LAM_STATUS.DATA_ROOM_VERIFY_F;
                    break;
                case JISIGHT_LAM_RECV_TYPE.ROOM_STATUS_T:           //32000 房间状态获取成功
                    if (jsonData.Mg)
                        eventArgs.message = jsonData.Mg;
                    debug.out("Get room status success.");
                    eventArgs.status = LAM_STATUS.DATA_ROOM_STATUS_T;
                    break;
                case JISIGHT_LAM_RECV_TYPE.ROOM_STATUS_F:           //32001 房间状态获取失败
                    eventArgs.message = "Get room status failed.";
                    debug.out("Get room status failed.");
                    eventArgs.status = LAM_STATUS.DATA_ROOM_STATUS_F;
                    break;
                case JISIGHT_LAM_RECV_TYPE.RANK_LIST_T:             //35000 接收排行榜成功
                    if (jsonData.Mg)
                        eventArgs.message = jsonData.Mg;
                    debug.out("Reveive rank list success.");
                    eventArgs.status = LAM_STATUS.DATA_RANK_LIST_T;
                    break;
                case JISIGHT_LAM_RECV_TYPE.RANK_LIST_F:             //35001 接收排行榜失败
                    eventArgs.message = "Reveive rank list failed.";
                    debug.out("Reveive rank list failed.");
                    eventArgs.status = LAM_STATUS.DATA_CUSTOM_MSG_F;
                    break;
                case JISIGHT_LAM_RECV_TYPE.AWARD_LIST_T:            //36000 接收奖励列表成功
                    if (jsonData.Mg)
                        eventArgs.message = jsonData.Mg;
                    debug.out("Request awarde list success.");
                    eventArgs.status = LAM_STATUS.DATA_AWARD_LIST_T;
                    break;
                case JISIGHT_LAM_RECV_TYPE.AWARD_LIST_F:            //36001 接收奖励列表失败
                    eventArgs.message = "Room verification success.";
                    debug.out("Request awarde list failed.");
                    eventArgs.status = LAM_STATUS.DATA_AWARD_LIST_F;
                    break;
                case JISIGHT_LAM_RECV_TYPE.CUSTOM_MSG_T:            //40000 接收自定义数据成功
                    if (jsonData.Mg)
                        eventArgs.message = jsonData.Mg;
                    debug.out("Push Custom message success.");
                    eventArgs.status = LAM_STATUS.DATA_CUSTOM_MSG_T;
                    break;
                case JISIGHT_LAM_RECV_TYPE.CUSTOM_MSG_F:            //40001 接收自定义数据失败
                    eventArgs.message = "Push Custom message failed.";
                    debug.out("Push Custom message failed.");
                    eventArgs.status = LAM_STATUS.DATA_CUSTOM_MSG_F;
                    break;
            }
            m_EventHandler(eventArgs);
        }
        debug.out("------ On WS MSG out ------");
    }

    function m_OnCloseFunc() {
        debug.out("------ OnClose in  ------");

        debug.out("------ OnClose out ------");
    }

    function m_OnErrorFunc() {
        debug.out("------ On Error in  ------");
        Console.log(result);
        debug.out("------ On Error out ------");
    }

    /***           _Post_房间验证          ***/
    function m_Post_RoomVerification(roomId, gameSign) {
        if (!roomId || !gameSign) {
            debug.outWarning("Your param is wrong, please check them : roomId, gameSign");
            debug.outWarning("[  roomId   ]: " + roomId);
            debug.outWarning("[ gameSign  ]: " + gameSign);
            return;
        }
        var _roomVrData = MsgPostRoomVerification(roomId, gameSign, m_userId);
        var bool = m_wsSndMsg(JSON.stringify(_roomVrData));
        if (bool == true)
            debug.out("RoomVerify snd complete.");
        else
            debug.out("RoomVerify snd failed.");
    }

    /***           _Get_房间状态           ***/
    function m_Get_GameStatus() {
        var _gGmStData = MsgGetGameStatus();
        var bool = m_wsSndMsg(JSON.stringify(_gGmStData));
        if (bool == true)
            debug.out("GameStatus snd complete.");
        else
            debug.out("GameStatus snd failed.");
    }

    /***           _Push_交互信息               ***/
    /***           Pc:["Calculator"]            ***/
    /***           H5:["userid0","userid1"]     ***/
    function m_Push_MsgInteraction(_toUserIds, _message) {
        var _pInteractionData = MsgPushInteraction(_toUserIds, _message);
        console.log(_pInteractionData);
        var bool = m_wsSndMsg(JSON.stringify(_pInteractionData));
        if (bool == true)
            debug.out("GameStatus snd complete.");
        else
            debug.out("GameStatus snd failed.");
    }

    function m_wsSndMsg(msgArgs) {
        if (m_webSocet != null && m_webSocet.readyState == WebSocket.OPEN) {
            debug.out(">>>>>>>>>>>  [Snd Original data] :" + msgArgs);
            m_webSocet.send(msgArgs);
            return true;
        }
        else {
            debug.outWarning(">>>>>>>>>>>  [Snd Original data] :" + msgArgs);
            debug.outWarning("WebSocket status is :" + m_webSocet.readyState + "\n" +
                "Please check your operation."
            );
            return false;
        }
    }

    function m_EventHandler(wsEventArgs) {
        if (wsEventArgs.status != LAM_STATUS.DEFALUT_VALUE) {
            if (m_lookSelf.JLAM_Event_Fir != null){
                m_lookSelf.JLAM_Event_Fir(wsEventArgs);
            }
        }
    }


}
